package org.jeecg.modules.gzgl.controller;

import cn.hutool.core.convert.Convert;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.zhaodui.fxy.*;
import lombok.extern.slf4j.Slf4j;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.util.RedisUtil;
import org.jeecg.modules.gzgl.api.apientity;
import org.jeecg.modules.gzgl.entity.*;
import org.jeecg.modules.gzgl.service.IFxyGzgl01Service;
import org.jeecg.modules.gzgl.service.IFxyGzgl04Service;
import org.jeecg.modules.gzgl.service.impl.FxyGzgl02ServiceImpl;
import org.jeecg.modules.gzgl.service.impl.FxyGzgl03ServiceImpl;
import org.jeecg.modules.gzgl.service.impl.FxyGzgl05ServiceImpl;
import org.jeecg.modules.gzgl.service.impl.FxyGzgl06ServiceImpl;
import org.jeecg.modules.system.service.impl.SysDataLogServiceImpl;
import org.jeecgframework.poi.excel.annotation.Excel;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import sun.util.resources.ar.CurrencyNames_ar_SY;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedHashMap;
import java.util.List;

/**
 * @Description: 公证订单主表
 * @Author: zhaodui
 * @Date: 2020-02-06
 * @Version: V1.0
 */
@RestController
@RequestMapping("/gzgl/fxyGzgl01")
@Slf4j
public class FxyGzgl01Controller extends JeecgController<FxyGzgl01, IFxyGzgl01Service> {
    @Autowired
    private IFxyGzgl01Service fxyGzgl01Service;
    @Autowired
    private SysDataLogServiceImpl sysDataLogService;
    @Autowired
    private FxyGzgl04Controller fxyGzgl04Controller;
    @Autowired
    private IFxyGzgl04Service fxyGzgl04Service;
    @Autowired
    private RedisUtil redisUtil;
    @Autowired
    private FxyGzgl02ServiceImpl fxyGzgl02Service;
    @Autowired
    private FxyGzgl03ServiceImpl fxyGzgl03Service;
    @Autowired
    private FxyGzgl05ServiceImpl fxyGzgl05Service;

    @Autowired
    private FxyGzgl06ServiceImpl fxyGzgl06Service;

    @Value(value = "${baseorg.common.basemobile}")
    private String basemobile;
    @Value(value = "${baseorg.common.dataurl}")
    private String dataurl;

    /**
     * 分页列表查询
     *
     * @param fxyGzgl01
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    @GetMapping(value = "/list")
    public Result<?> queryPageList(FxyGzgl01 fxyGzgl01,
                                   @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                   @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                   HttpServletRequest req) {
        QueryWrapper<FxyGzgl01> queryWrapper = QueryGenerator.initQueryWrapper(fxyGzgl01, req.getParameterMap());
        Page<FxyGzgl01> page = new Page<FxyGzgl01>(pageNo, pageSize);
        IPage<FxyGzgl01> pageList = fxyGzgl01Service.page(page, queryWrapper);
        return Result.ok(pageList);
    }

    /**
     * 添加
     *
     * @param fxyGzgl01
     * @return
     */
    @PostMapping(value = "/add")
    public Result<?> add(@RequestBody FxyGzgl01 fxyGzgl01) {

        fxyGzgl01Service.save(fxyGzgl01);
        Gson gs = new GsonBuilder().serializeNulls().create();
        String datacont = gs.toJson(fxyGzgl01);
        String id = fxyGzgl01.getId();
        sysDataLogService.addDataLog("fxy_gzgl_01", id, datacont);
        return Result.ok("添加成功！");
    }

    /**
     * 添加
     *
     * @return
     */
    @GetMapping(value = "/getdata")
    public Result<?> getdata(HttpServletRequest request, HttpServletResponse response,
                             @RequestParam(name = "query07", required = false) String query07) {
        ResulFxy resulFxy = new ResulFxy();


        log.info(query07);
        String url = dataurl;
        LinkedHashMap<String, Object> paramMap1 = new LinkedHashMap<String, Object>();
        LinkedHashMap<String, Object> paramMap2 = new LinkedHashMap<String, Object>();
        LinkedHashMap<String, Object> paramMap3 = new LinkedHashMap<String, Object>();
        LinkedHashMap<String, Object> paramMap4 = new LinkedHashMap<String, Object>();
        LinkedHashMap<String, Object> paramMap11 = new LinkedHashMap<String, Object>();
        String outlenth = "5";
        List<apientity.ResultBean> resultres1 =  new ArrayList<>();
        List<apientity.ResultBean> resultres2 = new ArrayList<>();
        List<apientity.ResultBean> resultres3 = new ArrayList<>();
        List<apientity.ResultBean> resultres4 = new ArrayList<>();
        List<apientity.ResultBean> resultres11 = new ArrayList<>();
        String htbl01="/";
        String htbl02="/";
        String htbl03="/";
        String htbl04="/";
        String htbl05="/";
        String htbl06="/";
        String htbl07="/";
        if (StringUtil.isNotEmpty(query07)) {
            deletebypara(query07);
        }
        try {//
            //生成主借人  自然人类型
            paramMap1.put("outlenth", outlenth);
            paramMap1.put("sqltype", "1");
            paramMap1.put("para", query07);
            String result1 = com.xiaoleilu.hutool.http.HttpUtil.post(url, paramMap1);
            apientity apientity1 = JSONHelper.fromJsonToObject(result1, org.jeecg.modules.gzgl.api.apientity.class);
            resultres1 = apientity1.getResult();
        } catch (Exception e) {
        }
        try {//生成 主借人  企业类型
            outlenth = "5";
            paramMap2.put("outlenth", outlenth);
            paramMap2.put("sqltype", "2");
            paramMap2.put("para", query07);
            String result2 = com.xiaoleilu.hutool.http.HttpUtil.post(url, paramMap2);
            apientity apientity2 = JSONHelper.fromJsonToObject(result2, org.jeecg.modules.gzgl.api.apientity.class);
            resultres2 = apientity2.getResult();
        } catch (Exception e) {
        }
        try {// 担保人
            outlenth = "9";
            paramMap3.put("outlenth", outlenth);
            paramMap3.put("sqltype", "3");
            paramMap3.put("para", query07);
            String result3 = com.xiaoleilu.hutool.http.HttpUtil.post(url, paramMap3);
            apientity apientity3 = JSONHelper.fromJsonToObject(result3, org.jeecg.modules.gzgl.api.apientity.class);
            resultres3 = apientity3.getResult();
        } catch (Exception e) {
        }
        try {//生成共借人
            outlenth = "5";
            paramMap4.put("outlenth", outlenth);
            paramMap4.put("sqltype", "4");
            paramMap4.put("para", query07);
            String result4 = com.xiaoleilu.hutool.http.HttpUtil.post(url, paramMap4);
            apientity apientity4 = JSONHelper.fromJsonToObject(result4, org.jeecg.modules.gzgl.api.apientity.class);
            resultres4 = apientity4.getResult();
        } catch (Exception e) {
        }
        try {//生成合同内容
            outlenth = "12";
            paramMap11.put("outlenth", outlenth);
            paramMap11.put("sqltype", "11");
            paramMap11.put("para", query07);
            String result11 = com.xiaoleilu.hutool.http.HttpUtil.post(url, paramMap11);
            apientity apientity11 = JSONHelper.fromJsonToObject(result11, org.jeecg.modules.gzgl.api.apientity.class);
            resultres11 = apientity11.getResult();
        } catch (Exception e) {

        }


        FxyGzgl04 t04 = new FxyGzgl04();//生成04合同主表
        t04.setQuery07(query07);//订单编号
        t04.setQuery08(query07);//合同编号
        t04.setQuery09("最高额借款合同");//合同名称
        t04.setQuery12("曹二众");//签署人姓名
        t04.setQuery13("13850081872");//签署人手机号
        t04.setQuery14("");//发起人用户ID

        t04.setQuery15("PS_TEST20200213");//流程号
        t04.setQuery19("0");//是否发送文书
        t04.setQuery20("1");//公证处标识
        t04.setQuery21("2000");//业务类型
        t04.setQuery22(query07);//文书编号
        t04.setQuery23("jkbzht_zd_test_202002_02");//模板编号
        t04.setQuery24("最高额度借款保证合同");//合同名称
        fxyGzgl04Service.save(t04);

        for (org.jeecg.modules.gzgl.api.apientity.ResultBean t2 : resultres1) {
            FxyGzgl02 t02 = new FxyGzgl02();
            t02.setQuery07(query07);//订单编号
            t02.setQuery08(query07);//合同编号
            t02.setQuery08("最高额借款合同");//合同名称
            t02.setQuery12(t2.getOutX5());//个人用户的手机号
            t02.setQuery13(t2.getOutX1());//用户姓名
            t02.setQuery14("111");//个人用户证件类型
            t02.setQuery15(t2.getOutX2());//个人用户证件号码
            t02.setQuery16("1");//公证处标识
            t02.setQuery17(t2.getOutX1());//邮箱
            t02.setQuery18(t2.getOutX3());//地址
            t02.setQuery19(t2.getOutX1());//第三方ID
            t02.setQuery22("未实名");//未实名
            t02.setQuery26("未签署");//未签署
            t02.setQuery27("主借人");//主借人
            fxyGzgl02Service.save(t02);

            //        借款人	htbl02
            htbl02 = t2.getOutX1();
            htbl03 = t2.getOutX2();
            htbl04 = t2.getOutX1();
            htbl05 = t2.getOutX3();
            htbl06 = t2.getOutX4();
            htbl07 = t2.getOutX5();
//        统一社会信用代码（组织机构代码）或身份证号码	htbl03
//        法定代表人/主要负责人	htbl04
//        现居住地	htbl05
//        户籍地	htbl06
//        电话	htbl07
        }

        for (org.jeecg.modules.gzgl.api.apientity.ResultBean t2 : resultres2) {
//                FxyGzgl02 t02 = new FxyGzgl02();
//                t02.setQuery07(t2.getOutX1());//订单编号
//                t02.setQuery12(t2.getOutX1());//个人用户的手机号
//                t02.setQuery13(t2.getOutX1());//用户姓名
//                t02.setQuery14(t2.getOutX1());//个人用户证件类型
//                t02.setQuery15(t2.getOutX1());//个人用户证件号码
//                t02.setQuery16(t2.getOutX1());//公证处标识
//                t02.setQuery17(t2.getOutX1());//邮箱
//                t02.setQuery18(t2.getOutX1());//地址
//                fxyGzgl02Service.save(t02);
        }

        for (org.jeecg.modules.gzgl.api.apientity.ResultBean t3 : resultres3) {
            String custype = t3.getOutX1();
            if("2".equals(custype)){
                FxyGzgl02 t02 = new FxyGzgl02();
                t02.setQuery07(query07);//订单编号
                t02.setQuery08(query07);//合同编号
                t02.setQuery08("最高额借款合同");//合同名称
                t02.setQuery12(t3.getOutX4());//个人用户的手机号
                t02.setQuery13(t3.getOutX2());//用户姓名
                t02.setQuery14("111");//个人用户证件类型
                t02.setQuery15(t3.getOutX5());//个人用户证件号码
                t02.setQuery16("1");//公证处标识
                t02.setQuery17(t3.getOutX4());//邮箱
                t02.setQuery18(t3.getOutX3());//地址
                t02.setQuery19(t3.getOutX4());//第三方ID
                t02.setQuery22("未实名");//未实名
                t02.setQuery26("未签署");//未签署
                t02.setQuery27("担保人");//主借人
                fxyGzgl02Service.save(t02);
            }
//            if("1".equals(custype)){
//                private String query16;
//                /**法定代表人证件号码*/
//                @Excel(name = "法定代表人证件号码", width = 15)
//                private String query17;
//                /**法定代表人电话*/
//                @Excel(name = "法定代表人电话", width = 15)
//                private String query18;
//                /**邮箱*/
//                @Excel(name = "邮箱", width = 15)
//                private String query19;
//                /**企业联系人姓名*/
//                @Excel(name = "企业联系人姓名", width = 15)
//                private String query20;
//                /**企业联系人电话*/
//                @Excel(name = "企业联系人电话", width = 15)
//                private String query21;
//                /**法定代表人姓名*/
//                @Excel(name = "法定代表人姓名", width = 15)
//                private String query22;
//                /**法定代表人手机号*/
//                @Excel(name = "法定代表人手机号", width = 15)
//                private String query23;
//                FxyGzgl03 t03 = new FxyGzgl03();
//                t03.setQuery07(query07);//订单编号
//                t03.setQuery08(query07);//合同编号
//                t03.setQuery08("最高额借款合同");//合同名称
//                t03.setQuery12(t3.getOutX2());//企业名称
//                t03.setQuery13(t3.getOutX8());//企业ID
//                t03.setQuery14("1");//公证处标识
//                t03.setQuery15(t3.getOutX7());//地址
//                t03.setQuery16("111");//个人用户证件类型
//                t03.setQuery17(t3.getOutX4());//邮箱
//                t03.setQuery18(t3.getOutX3());//地址
//                t03.setQuery19(t3.getOutX4());//第三方ID
//                t03.setQuery22("未实名");//未实名
//                t03.setQuery26("未签署");//未签署
//                t03.setQuery27("担保人");//主借人
//                fxyGzgl03Service.save(t03);
//            }
//                FxyGzgl02 t02 = new FxyGzgl02();
//                t02.setQuery07(t2.getOutX1());//订单编号
//                t02.setQuery12(t2.getOutX1());//个人用户的手机号
//                t02.setQuery13(t2.getOutX1());//用户姓名
//                t02.setQuery14(t2.getOutX1());//个人用户证件类型
//                t02.setQuery15(t2.getOutX1());//个人用户证件号码
//                t02.setQuery16(t2.getOutX1());//公证处标识
//                t02.setQuery17(t2.getOutX1());//邮箱
//                t02.setQuery18(t2.getOutX1());//地址
//                fxyGzgl02Service.save(t02);
        }

        //同时生成

//        版本号	htbl00
//        合同编号	htbl01
//        借款人	htbl02
//        统一社会信用代码（组织机构代码）或身份证号码	htbl03
//        法定代表人/主要负责人	htbl04
//        现居住地	htbl05
//        户籍地	htbl06
//        电话	htbl07

//        开始日期	htbl20
//        结束日期	htbl21
//        大写金额	htbl22
//        小写金额	htbl23
//        借款利率	htbl24
//        还款方式	htbl25
//        履约保证金比例	htbl26
//        履约保证金大写	htbl27
//        履约保证金小写	htbl28
//        其他特别约定	htbl29
//        合同正本份数	htbl30
//        贷款人份数	htbl31
//        借款人、保证人各执份数	htbl32
String htbl20="/";
String htbl21="/";
String htbl22="/";
String htbl23="/";
String htbl24="/";
String htbl25="/";
String htbl26="/";
String htbl27="/";
String htbl28="/";
String htbl29="/";
String htbl30="/";
String htbl31="/";
String htbl32="/";
        for (org.jeecg.modules.gzgl.api.apientity.ResultBean t11 : resultres11) {
            htbl01  = t11.getOutX4();
            for(int i=0;i<=33;i++){
                if(i==0){
                    FxyGzgl05 t05 = new FxyGzgl05();
                    t05.setQuery07(query07);//订单编号
                    t05.setQuery08(query07);//订单编号
                    t05.setQuery15("htbl00");//
                    t05.setQuery16("版本号");//
                    t05.setQuery17("2020(第一版)");//
                    fxyGzgl05Service.save(t05);
                }
                if(i==1){
                    FxyGzgl05 t05 = new FxyGzgl05();
                    t05.setQuery07(query07);//订单编号
                    t05.setQuery08(query07);//订单编号
                    t05.setQuery15("htbl01");//
                    t05.setQuery16("合同编号");//
                    t05.setQuery17(htbl01);//
                    fxyGzgl05Service.save(t05);
                }
                if(i==2){
                    FxyGzgl05 t05 = new FxyGzgl05();
                    t05.setQuery07(query07);//订单编号
                    t05.setQuery08(query07);//订单编号
                    t05.setQuery15("htbl02");//
                    t05.setQuery16("借款人");//
                    t05.setQuery17(htbl02);//
                    fxyGzgl05Service.save(t05);
                }

                if(i==3){
                    FxyGzgl05 t05 = new FxyGzgl05();
                    t05.setQuery07(query07);//订单编号
                    t05.setQuery08(query07);//订单编号
                    t05.setQuery15("htbl03");//
                    t05.setQuery16("统一社会信用代码（组织机构代码）或身份证号码");//
                    t05.setQuery17(htbl03);//
                    fxyGzgl05Service.save(t05);
                }

                if(i==4){
                    FxyGzgl05 t05 = new FxyGzgl05();
                    t05.setQuery07(query07);//订单编号
                    t05.setQuery08(query07);//订单编号
                    t05.setQuery15("htbl04");//
                    t05.setQuery16("法定代表人/主要负责人");//
                    t05.setQuery17(htbl04);//
                    fxyGzgl05Service.save(t05);
                }

                if(i==5){
                    FxyGzgl05 t05 = new FxyGzgl05();
                    t05.setQuery07(query07);//订单编号
                    t05.setQuery08(query07);//订单编号
                    t05.setQuery15("htbl05");//
                    t05.setQuery16("现居住地");//
                    t05.setQuery17(htbl05);//
                    fxyGzgl05Service.save(t05);
                }

                if(i==6){
                    FxyGzgl05 t05 = new FxyGzgl05();
                    t05.setQuery07(query07);//订单编号
                    t05.setQuery08(query07);//订单编号
                    t05.setQuery15("htbl06");//
                    t05.setQuery16("户籍地");//
                    t05.setQuery17(htbl06);//
                    fxyGzgl05Service.save(t05);
                }

                if(i==7){
                    FxyGzgl05 t05 = new FxyGzgl05();
                    t05.setQuery07(query07);//订单编号
                    t05.setQuery08(query07);//订单编号
                    t05.setQuery15("htbl07");//
                    t05.setQuery16("电话");//
                    t05.setQuery17(htbl07);//
                    fxyGzgl05Service.save(t05);
                }

                //        开始日期	htbl20
//        结束日期	htbl21
//        大写金额	htbl22
//        小写金额	htbl23
//        借款利率	htbl24
//        还款方式	htbl25
//        履约保证金比例	htbl26
//        履约保证金大写	htbl27
//        履约保证金小写	htbl28
//        其他特别约定	htbl29
//        合同正本份数	htbl30
//        贷款人份数	htbl31
//        借款人、保证人各执份数	htbl32
                htbl20 = t11.getOutX5();
                htbl21 = t11.getOutX6();
                htbl22 = Convert.digitToChinese(Integer.parseInt(t11.getOutX7()));
                htbl23 = t11.getOutX7();
//                htbl24 = t11.getOutX7();
//                htbl25 = t11.getOutX7();
//                htbl26 = t11.getOutX7();
//                htbl27 = t11.getOutX7();
//                htbl28 = t11.getOutX7();
                if(StringUtil.isNotEmpty(t11.getOutX11())){
                    htbl29 = t11.getOutX11();
                }
                htbl30 = t11.getOutX8();
                htbl31 = t11.getOutX9();
                htbl32 = t11.getOutX10();
                if(i==20){
                    FxyGzgl05 t05 = new FxyGzgl05();
                    t05.setQuery07(query07);//订单编号
                    t05.setQuery08(query07);//订单编号
                    t05.setQuery15("htbl20");//
                    t05.setQuery16("开始日期");//
                    t05.setQuery17(htbl20);//
                    fxyGzgl05Service.save(t05);
                }
                if(i==21){
                    FxyGzgl05 t05 = new FxyGzgl05();
                    t05.setQuery07(query07);//订单编号
                    t05.setQuery08(query07);//订单编号
                    t05.setQuery15("htbl21");//
                    t05.setQuery16("结束日期");//
                    t05.setQuery17(htbl21);//
                    fxyGzgl05Service.save(t05);
                }
                if(i==22){
                    FxyGzgl05 t05 = new FxyGzgl05();
                    t05.setQuery07(query07);//订单编号
                    t05.setQuery08(query07);//订单编号
                    t05.setQuery15("htbl22");//
                    t05.setQuery16("大写金额");//
                    t05.setQuery17(htbl22);//
                    fxyGzgl05Service.save(t05);
                }
                if(i==23){
                    FxyGzgl05 t05 = new FxyGzgl05();
                    t05.setQuery07(query07);//订单编号
                    t05.setQuery08(query07);//订单编号
                    t05.setQuery15("htbl23");//
                    t05.setQuery16("小写金额");//
                    t05.setQuery17(htbl23);//
                    fxyGzgl05Service.save(t05);
                }
                if(i==24){
                    FxyGzgl05 t05 = new FxyGzgl05();
                    t05.setQuery07(query07);//订单编号
                    t05.setQuery08(query07);//订单编号
                    t05.setQuery15("htbl24");//
                    t05.setQuery16("借款利率");//
                    t05.setQuery17(htbl24);//
                    fxyGzgl05Service.save(t05);
                }
                if(i==25){
                    FxyGzgl05 t05 = new FxyGzgl05();
                    t05.setQuery07(query07);//订单编号
                    t05.setQuery08(query07);//订单编号
                    t05.setQuery15("htbl25");//
                    t05.setQuery16("还款方式");//
                    t05.setQuery17(htbl25);//
                    fxyGzgl05Service.save(t05);
                }
                if(i==26){
                    FxyGzgl05 t05 = new FxyGzgl05();
                    t05.setQuery07(query07);//订单编号
                    t05.setQuery08(query07);//订单编号
                    t05.setQuery15("htbl26");//
                    t05.setQuery16("履约保证金比例");//
                    t05.setQuery17(htbl26);//
                    fxyGzgl05Service.save(t05);
                }
                if(i==27){
                    FxyGzgl05 t05 = new FxyGzgl05();
                    t05.setQuery07(query07);//订单编号
                    t05.setQuery08(query07);//订单编号
                    t05.setQuery15("htbl27");//
                    t05.setQuery16("履约保证金大写");//
                    t05.setQuery17(htbl27);//
                    fxyGzgl05Service.save(t05);
                }
                if(i==28){
                    FxyGzgl05 t05 = new FxyGzgl05();
                    t05.setQuery07(query07);//订单编号
                    t05.setQuery08(query07);//订单编号
                    t05.setQuery15("htbl28");//
                    t05.setQuery16("履约保证金小写");//
                    t05.setQuery17(htbl28);//
                    fxyGzgl05Service.save(t05);
                }
                if(i==29){
                    FxyGzgl05 t05 = new FxyGzgl05();
                    t05.setQuery07(query07);//订单编号
                    t05.setQuery08(query07);//订单编号
                    t05.setQuery15("htbl29");//
                    t05.setQuery16("其他特别约定");//
                    t05.setQuery17(htbl29);//
                    fxyGzgl05Service.save(t05);
                }
                if(i==30){
                    FxyGzgl05 t05 = new FxyGzgl05();
                    t05.setQuery07(query07);//订单编号
                    t05.setQuery08(query07);//订单编号
                    t05.setQuery15("htbl30");//
                    t05.setQuery16("合同正本份数");//
                    t05.setQuery17(htbl30);//
                    fxyGzgl05Service.save(t05);
                }
                if(i==31){
                    FxyGzgl05 t05 = new FxyGzgl05();
                    t05.setQuery07(query07);//订单编号
                    t05.setQuery08(query07);//订单编号
                    t05.setQuery15("htbl31");//
                    t05.setQuery16("贷款人份数");//
                    t05.setQuery17(htbl31);//
                    fxyGzgl05Service.save(t05);
                }
                if(i==32){
                    FxyGzgl05 t05 = new FxyGzgl05();
                    t05.setQuery07(query07);//订单编号
                    t05.setQuery08(query07);//订单编号
                    t05.setQuery15("htbl32");//
                    t05.setQuery16("借款人、保证人各执份数");//
                    t05.setQuery17(htbl32);//
                    fxyGzgl05Service.save(t05);
                }

            }

        }

        resulFxy.setResultCode("success");

        return Result.ok("添加成功！");
    }

    /**
     * 编辑
     *
     * @param fxyGzgl01
     * @return
     */
    @PutMapping(value = "/edit")
    public Result<?> edit(@RequestBody FxyGzgl01 fxyGzgl01) {

        if (StringUtil.isEmpty(fxyGzgl01.getQuery39()) || fxjConstant.iscommitfxy.equals(fxyGzgl01.getQuery39())) {//不提交法信
            fxyGzgl01Service.updateById(fxyGzgl01);
            return Result.ok("编辑成功！");
        }

        commitbatch(fxyGzgl01.getId());//批量提交合同到公证

        fxyGzgl01Service.updateById(fxyGzgl01);
        Gson gs = new GsonBuilder().serializeNulls().create();
        String datacont = gs.toJson(fxyGzgl01);
        String id = fxyGzgl01.getId();
        sysDataLogService.addDataLog("fxy_gzgl_01", id, datacont);
        return Result.ok("编辑成功!");
    }

    /**
     * 通过id删除
     *
     * @param id
     * @return
     */
    @DeleteMapping(value = "/delete")
    public Result<?> delete(@RequestParam(name = "id", required = true) String id) {
        fxyGzgl01Service.removeById(id);
        return Result.ok("删除成功!");
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @DeleteMapping(value = "/deleteBatch")
    public Result<?> deleteBatch(@RequestParam(name = "ids", required = true) String ids) {
        this.fxyGzgl01Service.removeByIds(Arrays.asList(ids.split(",")));
        return Result.ok("批量删除成功!");
    }

    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    @GetMapping(value = "/queryById")
    public Result<?> queryById(@RequestParam(name = "id", required = true) String id) {
        FxyGzgl01 fxyGzgl01 = fxyGzgl01Service.getById(id);
        if (fxyGzgl01 == null) {
            return Result.error("未找到对应数据");
        }
        return Result.ok(fxyGzgl01);
    }


    public void deletebypara(String para) {
//        QueryWrapper<FxyGzgl01> queryWrapper1 = new QueryWrapper<FxyGzgl01>();
//        queryWrapper1.eq("query07", para);
//        fxyGzgl01Service.remove(queryWrapper1);
//
//        QueryWrapper<FxyGzgl02> queryWrapper2 = new QueryWrapper<FxyGzgl02>();
//        queryWrapper2.eq("query07", para);
//        fxyGzgl02Service.remove(queryWrapper2);
//
//        QueryWrapper<FxyGzgl03> queryWrapper3 = new QueryWrapper<FxyGzgl03>();
//        queryWrapper3.eq("query07", para);
//        fxyGzgl03Service.remove(queryWrapper3);
//
//        QueryWrapper<FxyGzgl04> queryWrapper4 = new QueryWrapper<FxyGzgl04>();
//        queryWrapper4.eq("query07", para);
//        fxyGzgl04Service.remove(queryWrapper4);
//
//        QueryWrapper<FxyGzgl05> queryWrapper5 = new QueryWrapper<FxyGzgl05>();
//        queryWrapper5.eq("query07", para);
//        fxyGzgl05Service.remove(queryWrapper5);
//
//        QueryWrapper<FxyGzgl06> queryWrapper6 = new QueryWrapper<FxyGzgl06>();
//        queryWrapper6.eq("query07", para);
//        fxyGzgl06Service.remove(queryWrapper6);

        fxyGzgl01Service.deletebyquery07(para);
    }

    /**
     * 导出excel
     *
     * @param request
     * @param fxyGzgl01
     */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, FxyGzgl01 fxyGzgl01) {
        return super.exportXls(request, fxyGzgl01, FxyGzgl01.class, "公证订单主表");
    }

    /**
     * 通过excel导入数据
     *
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, FxyGzgl01.class);
    }

    public void commitbatch(String id) {
        FxyGzgl01 fxyGzgl01 = fxyGzgl01Service.getById(id);
        List<FxyGzgl04> list04 = fxyGzgl04Controller.get04by07(fxyGzgl01.getQuery07());
        FxyGzgl04 fxyGzgl04 = list04.get(0);
        try {
            String appCode = fxyGzgl01.getQuery01();
            fxyUser fxyUser = new fxyUser();

            fxyUser.setCaseNumber(fxyGzgl04.getQuery16());
            fxyUser.setUserId(fxyGzgl04.getQuery14());
            fxyUser.setOrgId(fxyGzgl04.getQuery20());
            fxyUser.setBusinessType(fxyGzgl04.getQuery21());
            fxyUser.setProcessNumber(fxyGzgl04.getQuery15());
            fxyUser.setSendAgreement(fxyGzgl04.getQuery19());
            List<fxyUser> fxyUserList = new ArrayList<>();
            String caseNumber = "";
            for (FxyGzgl04 tp : list04) {
                if(!StringUtil.isEmpty(tp.getQuery16())){//已经生成的就不需要再次生成
                    caseNumber = tp.getQuery16();
                    continue;
                }
                fxyUser fxy = new fxyUser();
                fxy.setAgreementCode(tp.getQuery22());
                fxy.setTemplateCode(tp.getQuery23());
                fxy.setAgreementName(tp.getQuery24());
                List<FxyGzgl05> fxyGzgl05List = new ArrayList<>();
                List<FxyGzgl02> fxyGzgl02List = new ArrayList<>();
                List<FxyGzgl03> fxyGzgl03List = new ArrayList<>();
                //如果数据为空则从系统获取
                //如果数据为空则从系统获取
                if (fxyGzgl02List == null || fxyGzgl02List.size() == 0) {
                    fxyGzgl02List = fxyGzgl04Controller.get02by0708(tp.getQuery07(), tp.getQuery08());
                }
                if (fxyGzgl03List == null || fxyGzgl03List.size() == 0) {
                    fxyGzgl03List = fxyGzgl04Controller.get03by0708(tp.getQuery07(), tp.getQuery08());
                }
                if (fxyGzgl05List == null || fxyGzgl05List.size() == 0) {
                    fxyGzgl05List = fxyGzgl04Controller.get05by0708(tp.getQuery07(), tp.getQuery08());
                }
                List<conmap> conmapList = new ArrayList<conmap>();
                if (fxyGzgl05List != null || fxyGzgl05List.size() > 0) {

                    for (FxyGzgl05 t : fxyGzgl05List) {
                        conmap con = new conmap();
                        con.setConKey(t.getQuery15());
                        con.setConValue(t.getQuery17());
                        conmapList.add(con);
                    }
                }
                //增加模板
                conmap con = new conmap();
                con.setConKey("userInfosTpl");
                JSONArray users = new JSONArray();
                JSONObject user = new JSONObject();
                con.setConValue("[[\"0\",\"1\",\"2\",\"2\",\"2\",\"2\"],[\"00\",\"11\",\"22\",\"2\",\"2\",\"2\"]]");
//                user.
//                con.setConValue(t.getQuery17());
                conmapList.add(con);
                fxy.setConmapList(conmapList);
                String userids = "";
                String signs = "";
                List<fxysign> listsign = new ArrayList<>();
                if (fxyGzgl02List != null || fxyGzgl02List.size() > 0) {

                    for (FxyGzgl02 t2 : fxyGzgl02List) {

                        fxysign fxysign = new fxysign();
                        if(StringUtil.isNotEmpty(t2.getQuery20())){
                            fxysign.setUserId(t2.getQuery20());
                        }else{
                            fxysign.setUserName(t2.getQuery13());
                            fxysign.setCardNo(t2.getQuery15());
                            fxysign.setMobile(t2.getQuery12());
                        }
//                        userids = userids + t2.getQuery20() + ",";
//                        signs = signs + t2.getQuery13() + ",";
                        listsign.add(fxysign);
                    }
                }
                if (fxyGzgl03List != null || fxyGzgl03List.size() > 0) {
                    for (FxyGzgl03 t3 : fxyGzgl03List) {
//                        userids = userids + t3.getQuery24() + ",";
//                        signs = signs + t3.getQuery12() + ",";
                        fxysign fxysign = new fxysign();
                        if(StringUtil.isNotEmpty(t3.getQuery24())){
                            fxysign.setUserId(t3.getQuery24());
                        }else{
//                            fxysign.setUserName(t3.getQuery22());
//                            fxysign.setCardNo(t3.getQuery17());
//                            fxysign.setMobile(t3.getQuery18());
                            fxysign.setCompanyName(t3.getQuery12());
                            fxysign.setContactsName(t3.getQuery20());
                            fxysign.setContactsPhone(t3.getQuery21());
                            fxysign.setBusinessLicenseId(t3.getQuery13());
                        }
//                        userids = userids + t2.getQuery20() + ",";
//                        signs = signs + t2.getQuery13() + ",";
                        listsign.add(fxysign);
                    }
                }
                fxy.setUserIds(userids);
                fxy.setSignatorys(signs);
                fxy.setSignList(listsign);
                fxyUserList.add(fxy);
            }
            agreementres res = fxyUtil.addAgreementByProcessNumberreturnobjectbatch(appCode, redisUtil, fxyUser, fxyUserList);
            if (!StringUtil.isEmpty(res.getResultCode())) {
                fxyGzgl04.setQuery16(res.getResultData().getCaseNumber());//案件编号
                fxyGzgl04.setQuery17(res.getResultData().getWritInfos().getWritNumber());//批次编号
                fxyGzgl04.setQuery18(res.getResultData().getWritInfos().getAgreementInfos().get(0).getStorageNo());//存证编号
                List<agreementres.ResultDataBean.WritInfosBean.AgreementInfosBean> writInfos = res.getResultData().getWritInfos().getAgreementInfos();
                for (FxyGzgl04 tpsave : list04) {
                    FxyGzgl04 fxyGzgl04save = new FxyGzgl04();
                    BeanUtils.copyProperties(tpsave, fxyGzgl04save);
                    fxyGzgl04save.setQuery16(fxyGzgl04.getQuery16());//案件编号
                    fxyGzgl04save.setQuery17(res.getResultData().getWritInfos().getWritNumber());//案件编号
                    String wsbh = fxyGzgl04save.getQuery22();//文书编号
                    for (agreementres.ResultDataBean.WritInfosBean.AgreementInfosBean ta : writInfos) {
                        if (ta.getAgreementCode().equals(wsbh)) {
                            fxyGzgl04save.setQuery18(ta.getStorageNo());//存证编号
                        }
                    }
                    fxyGzgl04Service.updateById(fxyGzgl04save);
                }
            }
        } catch (BaseBusinessException e) {

        }
    }

}
